//
//  BBSubtotalNodeView.m
//  xNose
//
//  Created by ben smith on 15/09/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBSubtotalNodeView.h"
#import "BBNode.h"
#import "BBDefaultController.h"
#import "BBNodeValuesView.h"
#import "BBSubtotalValuesView.h"

@implementation BBSubtotalNodeView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}

-(void)setNode:(BBNode*)aNode
{
	[node removeObserver:self forKeyPath:@"title"];
	[aNode retain];
	[node release];
	node = aNode;

	if (node != nil)
		[titleField setStringValue:node.title];
	[aNode addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];

	[self addSubviews];
}

-(void)addSubviews
{
	// add the kid views that will be the 'meat' of this view
	if (![self.node isCollection]) return;
	// now add my own total at the bottom
	float currentY = 0;
	float unitHeight = 75.0;
	NSRect valueRect = NSMakeRect(5, currentY, NSWidth([self bounds]) - 10, unitHeight);
	
	BBSubtotalValuesView * nodeValues = [[BBSubtotalValuesView alloc] initWithFrame:valueRect];
	[nodeValues setNode:self.node];
	[self addSubview:nodeValues];
	currentY += unitHeight + 10;
	[nodeValues release];
	
	for (BBNode* kid in [self.node.children reverseObjectEnumerator]) {
		valueRect.origin.y = currentY;
		BBNodeValuesView * kidNodeValues = [[BBNodeValuesView alloc] initWithFrame:valueRect];
		[kidNodeValues setNode:kid];
		[self addSubview:kidNodeValues];
		currentY += unitHeight + 10;	
		[kidNodeValues release];
	}
}

-(NSFont*)font 
{
	return [BBDefaultController boldMediumFont];
}

- (void)setupTextAttributes
{
	[super setupTextAttributes];
	[titleField setAlignment:NSCenterTextAlignment];
	[self addSubview:titleField];
}	

-(NSColor*)fontColor 
{
	return [NSColor blackColor];
}

- (void)drawNode
{    // Drawing code here.
//	[[NSColor redColor] set];
	
//	[NSBezierPath strokeRect:[self bounds]];
	
}

@end
